package com.ssmk.module.shoppingcar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ssmk.R;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SkuInfoBean2;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * author：郑志强
 * data：2017/7/7 11:47
 * function：
 */

public class WaitSendInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private final Context mContext;
    private final OrderAllBean.DataBean mDataBean;

    public WaitSendInfoAdapter(Context context, OrderAllBean.DataBean dataBean){
        this.mContext = context;
        this.mDataBean = dataBean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        //根据viewType生成viewHolder
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orderdetails_item_order, null);
                viewHolder = new ViewHolderOrder(view);
                break;
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orderdetails_item_user, null);
                viewHolder = new ViewHolderUser(view);
                break;
            case 2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orderdetails_item_goods, null);
                viewHolder = new ViewHolderGoods(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        List<OrderAllBean.DataBean.OrderInfoDetailListBean> orderInfoDetailList = mDataBean.getOrderInfoDetailList();
        Gson gson = new Gson();
        SkuInfoBean2 skuInfoBean = null;
        for (int i = 0; i <orderInfoDetailList.size(); i++) {
            String skuInfo = orderInfoDetailList.get(i).getSkuInfo();
            skuInfoBean = gson.fromJson(skuInfo, SkuInfoBean2.class);
        }

        //根据条目的类型给holder中的控件填充数据
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                ViewHolderOrder vh = (ViewHolderOrder) holder;
                vh.weixin.setVisibility(View.VISIBLE);
                vh.order_id.setText("订单号："+ mDataBean.getOrderCode());
                vh.order_pay.setText("已付款："+ mDataBean.getOrderPrice());
                vh.order_time.setText("创建时间："+ mDataBean.getCreateTime());
                vh.orderdetails.setText("支付号:"+ mDataBean.getPaySerialNum());
                break;
            case 1:
                ViewHolderUser vh1 = (ViewHolderUser) holder;
                vh1.user_name.setText(mDataBean.getDeliveryName());
                vh1.user_tel.setText(mDataBean.getDeliveryTel());
                vh1.order_remain.setText(mDataBean.getAddressDetail());

                break;
            case 2:
                ViewHolderGoods vh2 = (ViewHolderGoods) holder;
                Glide.with(mContext).load(skuInfoBean.getCommodityFormat().getGoodsFormatPhoto()).into(vh2.goods_iv);
                vh2.goods_name.setText(mDataBean.getOrderInfoDetailList().get(0).getGoodsName());
                vh2.goods_count.setText(mDataBean.getOrderInfoDetailList().get(0).getPurchaseSize()+"");
                vh2.goods_size.setText(skuInfoBean.getCommoditySizeJson().getSizeNumberName());
                vh2.goods_color.setText(skuInfoBean.getCommodityFormat().getGoodsFormatName());
                vh2.goods_price.setText("￥ "+mDataBean.getOrderInfoDetailList().get(0).getGoodsPrice());
                vh2.goods_priceall.setText("￥ "+mDataBean.getOrderInfoDetailList().get(0).getGoodsPrice());
                break;
        }
    }

    @Override
    public int getItemCount() {
        //获取条目数，模拟数据，这里是写死的
        return 3;
    }
    @Override
    public int getItemViewType(int position) {
        //跟据position对应的条目返回去对应的样式（Type）
        if (position ==0) {
            return 0;
        } else if (position==1) {
            return 1;
        } else return 2;
    }

    class ViewHolderOrder extends RecyclerView.ViewHolder {
        private final TextView order_id;
        private final TextView order_time;
        private final TextView order_pay;
        private final TextView orderdetails;

        ImageView weixin;
        public ViewHolderOrder(View itemView) {
            super(itemView);
            AutoUtils.auto(itemView);
            weixin= (ImageView) itemView.findViewById(R.id.orderdetails_weixin);

            order_id = (TextView)itemView.findViewById(R.id.orderdetails_order_id);
            order_time = (TextView)itemView.findViewById(R.id.orderdetails_order_time);
            order_pay = (TextView)itemView.findViewById(R.id.orderdetails_order_pay);
            orderdetails = (TextView)itemView.findViewById(R.id.orderdetails);
        }
    }

    class ViewHolderUser extends RecyclerView.ViewHolder {
        private final TextView user_name;
        private final TextView user_tel;
        private final TextView order_remain;

        public ViewHolderUser(View itemView) {
            super(itemView);
            AutoUtils.auto(itemView);

            user_name = (TextView)itemView.findViewById(R.id.orderdetails_user_name);
            user_tel = (TextView)itemView.findViewById(R.id.orderdetails_user_tel);
            order_remain = (TextView)itemView.findViewById(R.id.orderdetails_order_remain);
        }
    }

    class ViewHolderGoods extends RecyclerView.ViewHolder{
        private final ImageView goods_iv;
        private final TextView goods_name;
        private final TextView goods_count;
        private final TextView goods_size;
        private final TextView goods_color;
        private final TextView goods_price;
        private final TextView goods_priceall;

        public ViewHolderGoods(View itemView) {
            super(itemView);
            AutoUtils.auto(itemView);

            goods_iv = (ImageView)itemView.findViewById(R.id.orderdetails_goods_iv);
            goods_name = (TextView)itemView.findViewById(R.id.orderdetails_goods_name);
            goods_count = (TextView)itemView.findViewById(R.id.orderdetails_goods_count);
            goods_size = (TextView)itemView.findViewById(R.id.orderdetails_goods_size);
            goods_color = (TextView)itemView.findViewById(R.id.orderdetails_goods_color);
            goods_price = (TextView)itemView.findViewById(R.id.orderdetails_goods_price);
            goods_priceall = (TextView)itemView.findViewById(R.id.orderdetails_goods_priceall);

        }
    }
}
